package cn.edu.xjtu.data_structure.experiment.ex_1;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author yjq
 * @version 1.0
 * @date 2021/10/22 18:57
 */
public class MyList<T> implements List<T> {
    //顺序表实现
    private int maxSize;//表的最大大小
    private int numInList;//表中的实际元素数
    private int curr;//当前元素的位置
    private T[] listArray; //包含所有元素的数组
    private File result = new File("src/result.txt");
    //运行结果存储在文本文件中
    FileWriter fw = new FileWriter(result);
    BufferedWriter bw = new BufferedWriter(fw);


    public MyList(int sz) throws IOException {
        maxSize = sz;
        curr = -1;
        numInList=0;
        listArray = (T[]) new Object[sz];
    }//构造器



    @Override
    public void insert(T newElement) {//插入元素
        if (!isEmpty()) {
            for (int i = numInList; i > curr; i--) {
                listArray[i] = listArray[i - 1];
            }
        }//元素后移
        listArray[++curr] = newElement;//游标加一
        numInList++;
    }

    @Override
    public void remove() {//删除元素
        if (curr == numInList - 1) {
            gotoBeginning();//被删除元素在末尾则将游标移动到开头
        } else {
            for (int i = curr; i < numInList; i++) {
                listArray[i] = listArray[i + 1];
            }
        }
        numInList--;
    }

    @Override
    public void replace(T newElement) {//替换元素
            listArray[curr] = newElement;
    }

    @Override
    public void clear() {//清空所有元素
         curr = -1;
         numInList=0;
    }

    @Override
    public boolean isEmpty() {//判断是否为空
        return numInList == 0;
    }

    @Override
    public boolean isFull() {//判断是否为满
        return numInList >= maxSize;
    }

    @Override
    public boolean gotoBeginning() {//将游标移动到链表开始
        if (!isEmpty()) {
            curr = 0;
            return true;
        }
        return false;
    }

    @Override
    public boolean gotoEnd() {//将游标移动到链表末尾
        if (!isEmpty()) {
            curr = numInList-1;
            return true;
        }
        return false;
    }

    @Override
    public boolean gotoNext() {//将游标移动到下一个结点
        if (curr != numInList - 1) {
            curr++;
            return true;
        }
        return false;
    }

    @Override
    public boolean gotoPrev() {//将游标移动到前一个结点
        if (curr != 0) {
            curr--;
            return true;
        }
        return false;
    }

    @Override
    public T getCursor() {//获取当前游标位置的结点值
        return listArray[curr];
    }

    @Override
    public void showStructure() throws IOException {//输出列表中的元素和游标的值
        if (isEmpty()) {
            System.out.print("Empty list ");
            bw.write("Empty list ");
        } else {
            for (int i = 0; i < numInList ; i++) {
                System.out.print(listArray[i]+" ");
                bw.write(listArray[i]+" ");
            }
        }
        System.out.println(curr);
        bw.write(curr+"");
        bw.newLine();
    }
}

